#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/3/1 9:59 AM
# @Author  : Mr.Dynasty
# @File    : game.py
# @Software: PyCharm
# @license : Copyright(C), Mr.Dynasty

import pygame
from pygame.locals import *
import time, random

class Bullet(object):

    def __init__(self, screen_temp, bullet_image, x=0, y=0):
        self.x = x
        self.y = y
        self.screen_temp = screen_temp
        self.bullet_image = pygame.image.load(bullet_image)
        rect = self.bullet_image.get_rect()
        self.width = rect.width
        self.height = rect.height

    def display(self):
        self.screen_temp.blit(self.bullet_image, (self.x, self.y))

class HeroBullet(Bullet):

    def move(self, speed):
        self.y -= speed
        if self.y <= 0:#超出屏幕
            return False
        return True

class EnmeyBullet(Bullet):

    def move(self, speed):
        self.y += speed
        if self.y >= 768:#超出屏幕
            return False
        return True

class Plane(object):

    def __init__(self, screen_temp, plane_iamge, x=0, y=0,):
        self.x = x
        self.y = y
        self.screen_temp = screen_temp
        self.palane_image = pygame.image.load(plane_iamge)
        rect = self.palane_image.get_rect()
        self.width = rect.width
        self.height = rect.height
        self.bullet_list = []#存储自己打出去的炮弹

    def collsion_plane(self, plane):
        '''飞机碰撞检测'''
        if (self.x + self.width > plane.x and self.x + self.width < plane.x + plane.width) or (plane.x + plane.width > self.x and plane.x + plane.width < self.x + self.width):
            if self.y + self.height > plane.y:
                return True
        return False

    def disappear(self):
        #消失动画
        count = 0
        while count < 4:
            bo_image = pygame.image.load('./images/bo{}.png'.format(str(count)))
            count += 1
            self.screen_temp.blit(bo_image, (self.x + self.width / 2.0, self.y + self.height / 2))


    def display(self):
        self.screen_temp.blit(self.palane_image, (self.x, self.y))

    def fire(self, ishero=True):
        if ishero:
            y = self.y
            bullet = HeroBullet(self.screen_temp, './images/pd.png', self.x + self.width / 2.0, y)
        else:
            y = self.y + self.height
            bullet = EnmeyBullet(self.screen_temp, './images/pd.png', self.x + self.width / 2.0, y)
        self.bullet_list.append(bullet)

class HeroPlane(Plane):

    def move_left(self, speed):
        self.x -= speed
        if self.x <= 0:
            self.x = 0

    def move_right(self, speed):
        self.x += speed
        if self.x >= 512 - 106:
            self.x = 512 - 106

    def move_up(self, speed):
        self.y -= speed
        if self.y <= 0:
            self.y = 0

    def move_down(self, speed):
        self.y += speed
        if self.y >= 768 - 76:
            self.y = 768 - 76

    def bullet_collsion(self, bullet_list):
        for bullet in bullet_list:
            if bullet.x > self.x and bullet.x < self.x + self.width and bullet.y + bullet.height > self.y:
                bullet_list.remove(bullet)
                # 有碰撞
                return True
        return False


class EnmeyPlane(Plane):

    def move(self, speed):
        self.y += speed
        if self.y >= 768:
            return False
        else:
            return True

    def bullet_collsion(self, bullet_list):
        for bullet in bullet_list:
            if bullet.x > self.x and bullet.x < self.x + self.width and bullet.y < self.y + self.height:
                bullet_list.remove(bullet)
                return True
        return False


def screen_show(x,y):
    screen = pygame.display.set_mode((512, 768), 0, 0)
    background = pygame.image.load('./images/bg2.jpg')
    screen.blit(background, (x, y))
    return screen

offset = {K_LEFT:0, K_RIGHT:0, K_UP:0, K_DOWN:0, K_a:0, K_d:0, K_w:0, K_s:0}

def key_control(hero, speed=5):
    #执行退出操作
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    #获取按键信息
    pressed_keys = pygame.key.get_pressed()
    if pressed_keys[K_LEFT] or pressed_keys[K_a]:
        hero.move_left(speed)
    elif pressed_keys[K_RIGHT] or pressed_keys[K_d]:
        hero.move_right(speed)
    elif pressed_keys[K_UP] or pressed_keys[K_w]:
        hero.move_up(speed)
    elif pressed_keys[K_DOWN] or pressed_keys[K_s]:
        hero.move_down(speed)

    if pressed_keys[K_SPACE]:
        hero.fire()

def append_bullet_list(total_list, bullet_list):
    for bullet in bullet_list:
        if bullet not in total_list:
            total_list.append(bullet)


def main():

    screen_y = -768
    enmey_list = []
    enmey_bullet_list = []
    hero = None
    while True:
        # 画布绘制
        if screen_y < 0:
            screen = screen_show(0, screen_y)
            screen_y += 2
        else:
            screen_y = -768
        # 英雄飞机
        if not hero:
            hero = HeroPlane(screen, './images/me.png', (512 - 106) / 2.0, 768 - 76 - 10)
        key_control(hero)
        if hero.bullet_collsion(enmey_bullet_list):
            # 敌机子弹碰撞检测 英雄坠落
            hero.disappear()
            print('游戏结束!')
            exit()

        #英雄子弹
        for bullet in hero.bullet_list:
            if bullet.move(5):
                bullet.display()
            else:
                hero.bullet_list.remove(bullet)
        #修改子弹与飞机位置
        hero.display()
        # 敌方飞机
        if random.choice(range(0, 50)) == 25:
            #添加敌机
            enmey_position_x = random.choice(range(0, 406))
            enmey = EnmeyPlane(screen, './images/e{}.png'.format(random.choice(range(0, 3))), enmey_position_x, -10)
            enmey_list.append(enmey)
        for em in enmey_list:
            if not em.move(1):
                enmey_list.remove(em)
            em.display()
            #敌机开火
            if random.choice(range(0, 100)) == 25:
                em.fire(ishero=False)
                append_bullet_list(enmey_bullet_list, em.bullet_list)
            #敌机中弹坠落
            if em.bullet_collsion(hero.bullet_list):
                em.disappear()
                enmey_list.remove(em)
            #飞机碰撞坠落
            if em.collsion_plane(hero):
                em.disappear()
                hero.disappear()
                enmey_list.remove(em)
                print('游戏结束！')
                exit()

        #敌方子弹飞行
        for bullet in enmey_bullet_list:
            if bullet.move(3):
                bullet.display()
            else:
                enmey_bullet_list.remove(bullet)

        pygame.display.update()
        time.sleep(0.03)

if __name__ == '__main__':
    main()



